package cn.yuanqiao.cqbc.fileBackup.util;

import cn.yuanqiao.common.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * @version 1.0
 * @author： xy
 * @date： 2023-03-02 11:35
 */

@Component
public class RouteUtil {
    private final static ForkJoinPool forkJoinPool = new ForkJoinPool();

    //获取服务器路径
    public List getRoute(String path) {
        List list = new ArrayList();
        if (StringUtils.isBlank(path)) {
            File[] files = File.listRoots();
            if (files.length > 0) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        Map map = new HashMap();
                        map.put("name", file.getAbsolutePath());
                        map.put("size", "");
                        map.put("type", "文件夹");
                        map.put("isRootPath", "1");
                        list.add(map);
                    }
                }
            }
        } else {
            File file = new File(path);
            File[] files = file.listFiles();
            if (files!=null&&files.length > 0) {
                for (File file1 : files) {
                    if (file1.isDirectory()) {
                        Map map = new HashMap();
                        map.put("name", file1.getAbsolutePath());
                        map.put("size", "");
                        map.put("type", "文件夹");
                        map.put("isRootPath", "0");
                        list.add(map);
                    }
                }
            }

        }

        return list;
    }

    /**
     * 调用获取文件夹大小方法
     *
     * @return
     */
    public String getFileSize(String path, String isRootPath) {
        long total = 0l;
        if (isRootPath.equals("1")) {
            File file = new File(path);
            //获取磁盘大小
            total = file.getUsableSpace();
        } else {
            //获取文件夹大小
            total = forkJoinPool.invoke(new FileSizeFinder(new File(path)));
        }
        return getNetFileSizeDescription(total);
    }

    /**
     * 获取文件夹大小
     */
    private static class FileSizeFinder extends RecursiveTask<Long> {
        final File file;

        public FileSizeFinder(final File theFile) {
            file = theFile;
        }

        @Override
        public Long compute() {
            long size = 0;
            if (file.isFile()) {
                size = file.length();
            } else {
                final File[] children = file.listFiles();
                if (children != null) {
                    List<ForkJoinTask<Long>> tasks = new ArrayList<ForkJoinTask<Long>>();
                    for (final File child : children) {
                        if (child.isFile()) {
                            size += child.length();
                        } else {
                            tasks.add(new FileSizeFinder(child));
                        }
                    }
                    for (final ForkJoinTask<Long> task : invokeAll(tasks)) {
                        size += task.join();
                    }
                }
            }
            return size;
        }
    }


    /**
     * 把字节转化为KB、MB、GB的方法
     *
     * @param size
     * @return
     */
    public String getNetFileSizeDescription(long size) {
        StringBuffer bytes = new StringBuffer();
        DecimalFormat format = new DecimalFormat("###.0");
        if (size >= 1024 * 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0 * 1024.0));
            bytes.append(format.format(i)).append("GB");
        } else if (size >= 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0));
            bytes.append(format.format(i)).append("MB");
        } else if (size >= 1024) {
            double i = (size / (1024.0));
            bytes.append(format.format(i)).append("KB");
        } else if (size < 1024) {
            if (size <= 0) {
                bytes.append("0B");
            } else {
                bytes.append((int) size).append("B");
            }
        }
        return bytes.toString();
    }
}
